package windelpdv;

import br.com.daruma.jna.DUAL;
import br.com.daruma.jna.UTIL;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.util.Properties;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;

/**
 *
 * @author Marcio
 */
public class frmPdv extends javax.swing.JFrame {
    
    //Vector vetor_itens;
    private static Properties config = new Properties();
    private static String arquivo = "c:/Marcio/config.ini";
    //private static String arquivo = "c:/Users/Marcio/Projetos Java/WindelPDV/config.ini";
    int iRetorno;
    private Connection ConexaoWindel = null;
    private Connection ConexaoPDVWindel = null;
    private ResultSet rsWindel = null;
    int quant = 1;
    public float Subtotal = 0;
    public int CodigoItem = 1;
    public int CodigoCupom = 1;
    Vector vetor_itens = new Vector();//armazena dados para lancamento tabela VENDAS
    DefaultTableModel tabela = new DefaultTableModel(null, new String[]{"Cód", "Produto", "Quant", "Preço", "Total"});

    public frmPdv(Connection conexaoWindel, Connection conexaoPDVWindel) {
        initComponents();
        this.ConexaoWindel = conexaoWindel;
        this.ConexaoPDVWindel = conexaoPDVWindel;
        tfCodBarras.grabFocus();
        defineRenderers();
        
        try {
            config.load(new FileInputStream(arquivo));
            iRetorno = UTIL.eDefinirProduto(config.getProperty("PRODUTO"));
            iRetorno = DUAL.regPortaComunicacao(config.getProperty("PORTA"));
            iRetorno = DUAL.regVelocidade(config.getProperty("VELOCIDADE"));
            iRetorno = DUAL.regTermica(config.getProperty("TERMICA"));
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex, "Erro", JOptionPane.INFORMATION_MESSAGE);
        }
        
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tfDescricao = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tfCodBarras = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfPreco = new javax.swing.JTextField();
        tfPreco.setHorizontalAlignment(tfPreco.RIGHT);
        jLabel4 = new javax.swing.JLabel();
        tfQuantidade = new javax.swing.JTextField();
        tfQuantidade.setHorizontalAlignment(tfQuantidade.RIGHT);
        jLabel5 = new javax.swing.JLabel();
        tfTotalItem = new javax.swing.JTextField();
        tfTotalItem.setHorizontalAlignment(tfTotalItem.RIGHT);
        jLabel6 = new javax.swing.JLabel();
        tfSubtotal = new javax.swing.JTextField();
        tfSubtotal.setHorizontalAlignment(tfSubtotal.RIGHT);
        jLabel7 = new javax.swing.JLabel();
        tfQuant = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbProdutos = new javax.swing.JTable();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("PDV");

        tfDescricao.setEditable(false);
        tfDescricao.setFont(new java.awt.Font("Times New Roman", 0, 48)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel2.setText("Código de Barras:");

        tfCodBarras.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        tfCodBarras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfCodBarrasActionPerformed(evt);
            }
        });
        tfCodBarras.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfCodBarrasKeyPressed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
        jLabel3.setText("Preço Unit.");

        tfPreco.setEditable(false);
        tfPreco.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
        tfPreco.setText("0,00");

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
        jLabel4.setText("Quant.");

        tfQuantidade.setEditable(false);
        tfQuantidade.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N

        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
        jLabel5.setText("Total Item");

        tfTotalItem.setEditable(false);
        tfTotalItem.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
        tfTotalItem.setText("0,00");

        jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
        jLabel6.setText("Subtotal");

        tfSubtotal.setEditable(false);
        tfSubtotal.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
        tfSubtotal.setText("0,00");

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/windelpdv/imagens/Logo2.jpg"))); // NOI18N

        tfQuant.setEditable(false);
        tfQuant.setFont(new java.awt.Font("Arial", 0, 48)); // NOI18N
        tfQuant.setText("1X");

        jScrollPane2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        tbProdutos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cód", "Produto", "Quant", "Preço", "Total"
            }
        ));
        tbProdutos.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        tbProdutos.setEnabled(false);
        jScrollPane2.setViewportView(tbProdutos);

        jLabel8.setText("F2 - Quantidade");

        jLabel9.setText("F4 - Consulta Produto");

        jLabel10.setText("F5 - Excui Produto");

        jLabel11.setText("F12 - Encerra Cupom");

        jLabel1.setText("F9 - Abre Caixa");

        jLabel12.setText("F8 - Retirada Caixa");

        jLabel13.setText("F7 - Fecha Caixa");

        jLabel14.setText("F6 - Abre Gaveta");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(tfCodBarras, javax.swing.GroupLayout.PREFERRED_SIZE, 388, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel7))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tfQuant, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 879, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 628, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(39, 39, 39)
                                .addComponent(jLabel8)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel9)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel10)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel14)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel11)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6))
                                .addGap(0, 14, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel13)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfSubtotal, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfTotalItem, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfPreco, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel12))))
                .addContainerGap(40, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfCodBarras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel7)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfQuant, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(tfQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfPreco, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfTotalItem, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(183, 183, 183)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfSubtotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(16, 16, 16))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12)
                    .addComponent(jLabel13)
                    .addComponent(jLabel1)
                    .addComponent(jLabel14))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfCodBarrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfCodBarrasActionPerformed
        Statement st;

        try {
            st = ConexaoWindel.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

            if (tfCodBarras.getText().substring(0, 1).equals("2")) {
                String codBalanca = tfCodBarras.getText().substring(2, 7);
                rsWindel = st.executeQuery("Select DESCRICAO, IDPRODUTO, VLR_VENDA, BARRAS from produtos WHERE IDPRODUTO like\'%" + codBalanca + "\'");
                if (rsWindel.next()) {

                    String totalItemBalanca = tfCodBarras.getText().substring(7, 12);
                    String form = totalItemBalanca.substring(0, 3) + "." + totalItemBalanca.substring(3, 5);
                    float TotalItem = Float.parseFloat(form);
                    float proValorVenda = Float.parseFloat((rsWindel.getString("VLR_VENDA")));
                    float peso = TotalItem / Float.valueOf(rsWindel.getString("VLR_VENDA"));
                    String pesoForm = String.valueOf(peso).substring(0, 5);

                    Subtotal = TotalItem + Subtotal;
                    ItemVenda item = new ItemVenda(CodigoItem, CodigoCupom, rsWindel.getString("IDPRODUTO"), rsWindel.getString("DESCRICAO"),
                            pesoForm, proValorVenda, TotalItem, Subtotal);
                    
                    ItemVenda itemVetor = new ItemVenda();
                    itemVetor.setDescricao(rsWindel.getString("DESCRICAO"));
                    itemVetor.setCodBarras(rsWindel.getString("BARRAS"));
                    itemVetor.setQuantVetor(pesoForm);
                    itemVetor.setPrecoUnit(proValorVenda);
                    itemVetor.setPrecoTotalItem(TotalItem);
                    vetor_itens.addElement(itemVetor);
                    
                    tabela.addRow(new String[]{String.valueOf(item.getCodigo()), item.getDescricao(), item.getPeso(), formatarFloat(item.getPrecoUnit()), formatarFloat(item.getPrecoTotalItem())});
                    tbProdutos.setModel(tabela);
                    defineRenderers();
                   
                    tfPreco.setText(formatarFloat(proValorVenda));
                    tfDescricao.setText(rsWindel.getString("DESCRICAO"));
                    tfTotalItem.setText(formatarFloat(TotalItem));
                    tfQuantidade.setText(pesoForm.replace(".", ","));
                    tfSubtotal.setText(formatarFloat(Subtotal));
                    tfCodBarras.setText("");
                    quant = 1;
                    tfQuant.setText("1X");
                    CodigoItem = CodigoItem + 1;
                }
            } else {

                rsWindel = st.executeQuery("Select DESCRICAO, BARRAS, IDPRODUTO, VLR_VENDA from produtos WHERE BARRAS =\'" + tfCodBarras.getText() + "\'");
                if (rsWindel.next()) {
                    float proValorVenda = Float.parseFloat((rsWindel.getString("VLR_VENDA")));
                    float TotalItem = proValorVenda * quant;
                    Subtotal = TotalItem + Subtotal;
                    ItemVenda item = new ItemVenda(CodigoItem, CodigoCupom, rsWindel.getString("IDPRODUTO"), rsWindel.getString("BARRAS"), rsWindel.getString("DESCRICAO"),
                            quant, proValorVenda, TotalItem, Subtotal);
                    
                    ItemVenda itemVetor = new ItemVenda();
                    itemVetor.setDescricao(rsWindel.getString("DESCRICAO"));
                    itemVetor.setCodBarras(rsWindel.getString("BARRAS"));
                    itemVetor.setQuantVetor(String.valueOf(quant));
                    itemVetor.setPrecoUnit(proValorVenda);
                    itemVetor.setPrecoTotalItem(TotalItem);
                    vetor_itens.addElement(itemVetor);
                    
                    tabela.addRow(new String[]{String.valueOf(item.getCodigo()), item.getDescricao(), String.valueOf(item.getQuant()), formatarFloat(item.getPrecoUnit()), formatarFloat(item.getPrecoTotalItem())});
                    tbProdutos.setModel(tabela);
                    defineRenderers();

                    tfPreco.setText(formatarFloat(proValorVenda));
                    tfDescricao.setText(rsWindel.getString("DESCRICAO"));
                    tfTotalItem.setText(formatarFloat(TotalItem));
                    tfQuantidade.setText("" + quant);
                    tfSubtotal.setText(formatarFloat(Subtotal));
                    tfCodBarras.setText("");
                    quant = 1;
                    tfQuant.setText("1X");
                    CodigoItem = CodigoItem + 1;

        
        
                } else {
                    JOptionPane.showMessageDialog(null, "Produto não cadastrado!", "Atenção", JOptionPane.INFORMATION_MESSAGE);
                    tfCodBarras.setText("");
                }
            }
            GuiUtils.selectAndScroll(tbProdutos, tbProdutos.getRowCount()-1);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Não foram encontrados Registros!", "Erro", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_tfCodBarrasActionPerformed

    private void tfCodBarrasKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfCodBarrasKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_F2) {
            String quantSt = JOptionPane.showInputDialog(null, "Digite a quantidade", "Atençao!", JOptionPane.INFORMATION_MESSAGE);
            quant = Integer.parseInt(quantSt);
            tfQuant.setText(quant + "X");
        }

        if (evt.getKeyCode() == KeyEvent.VK_F4) {
            dlgLocalizaProduto dlg = new dlgLocalizaProduto(this, true, ConexaoWindel);
            if (dlg.alterarDados()) {
                tfCodBarras.setText(dlg.getBarras());
                tfCodBarrasActionPerformed(null);
            }
            dlg.dispose();
        }
        if (evt.getKeyCode() == KeyEvent.VK_F6) {
                int iRetorno;
                iRetorno = DUAL.iImprimirTexto("<g></g>",0);
        }
        
        if (evt.getKeyCode() == KeyEvent.VK_F9) {
                IniciaCaixa frm = new IniciaCaixa(ConexaoPDVWindel);
                frm.setVisible(true);            
        }
        
        if (evt.getKeyCode() == KeyEvent.VK_F7) {
                frmFechaCaixa frm = new frmFechaCaixa(ConexaoPDVWindel);
                frm.setVisible(true);            
        }
        
        if (evt.getKeyCode() == KeyEvent.VK_F8) {
                frmRetiradaCaixa frm = new frmRetiradaCaixa(ConexaoPDVWindel);
                frm.setVisible(true);
        }

        if (evt.getKeyCode() == KeyEvent.VK_F12) {
            dlgFinalizaCupom dlg = new dlgFinalizaCupom(this, true, Subtotal, tabela, ConexaoPDVWindel, vetor_itens);
            if (dlg.finalizarCupom()) {
                Subtotal = 0;
                CodigoItem = 1;
                CodigoCupom = 1;
                tabela.setNumRows(0);
                tbProdutos.setModel(tabela);
                tfDescricao.setText("");
                tfPreco.setText("");
                tfQuantidade.setText("");
                tfSubtotal.setText("");
                tfTotalItem.setText("");
            }
            dlg.dispose();
        }

        if (evt.getKeyCode() == KeyEvent.VK_F5) {
            String ItemExcluido = JOptionPane.showInputDialog(null, "Digite o número do item a ser excluido", "Atençao!", JOptionPane.INFORMATION_MESSAGE);

            int TamanhoTabela = tabela.getRowCount(); //pega tamanho da tabela
            int temItem = -1;
            //for para saber se o codigo digitado pelo usuario fecha com algum do jtable
            //se tiver. temItem = código do item, senão temItem continua -1
            for (int i = 0; i < TamanhoTabela; i++) {
                if ((tabela.getValueAt(i, 0)).equals(ItemExcluido)) {
                    temItem = i;
                }
            }
            if (temItem == -1) {
                JOptionPane.showMessageDialog(null, "Código não existe",
                        "Menssagem", JOptionPane.INFORMATION_MESSAGE);
            } else {
                String teste = String.valueOf(tabela.getValueAt(temItem, 4));
                teste = teste.replaceAll(",", ".");
                Subtotal = Subtotal - Float.valueOf(teste);
                tfSubtotal.setText(formatarFloat(Subtotal));
                tabela.removeRow(temItem);
                tbProdutos.setModel(tabela);
            }
        }
    }//GEN-LAST:event_tfCodBarrasKeyPressed

    public String getFormatDec(String snumero) {
        snumero = snumero.replaceAll(",", ".");
        DecimalFormat df = new DecimalFormat("0.00");
        snumero = df.format(Double.parseDouble(snumero));
        snumero = snumero.replaceAll(",", ".");
        return snumero;
    }

    public String formatarFloat(float numero) {
        String retorno = "";
        DecimalFormat formatter = new DecimalFormat("0.00");
        try {
            retorno = formatter.format(numero);
        } catch (Exception ex) {
            System.err.println("Erro ao formatar numero: " + ex);
        }
        return retorno;
    }
    
    /*    private void rolarScroll() {  
        SwingUtilities.invokeLater(new Runnable() {  
            public void run() {  
                JScrollBar temp = getMeuScroll().getVerticalScrollBar();  
                temp.setValue(temp.getMaximum());  
            }  
        });  
    }  */

    private void defineRenderers() {
        DefaultTableCellRenderer rendererCentro = new DefaultTableCellRenderer();
        rendererCentro.setHorizontalAlignment(SwingConstants.CENTER);
        DefaultTableCellRenderer rendererDireita = new DefaultTableCellRenderer();
        rendererDireita.setHorizontalAlignment(SwingConstants.RIGHT);
        DefaultTableCellRenderer rendererEsquerda = new DefaultTableCellRenderer();
        rendererEsquerda.setHorizontalAlignment(SwingConstants.LEFT);

        JTableHeader header = tbProdutos.getTableHeader();
        header.setPreferredSize(new Dimension(0, 15));
        TableColumnModel modeloDaColuna = tbProdutos.getColumnModel();

        modeloDaColuna.getColumn(0).setCellRenderer(rendererCentro);
        modeloDaColuna.getColumn(1).setCellRenderer(rendererEsquerda);
        modeloDaColuna.getColumn(2).setCellRenderer(rendererCentro);
        modeloDaColuna.getColumn(3).setCellRenderer(rendererCentro);
        modeloDaColuna.getColumn(4).setCellRenderer(rendererCentro);

        modeloDaColuna.getColumn(0).setMaxWidth(50);
        //modeloDaColuna.getColumn(1).setMinWidth(300);
        modeloDaColuna.getColumn(2).setMaxWidth(100);
        modeloDaColuna.getColumn(3).setMaxWidth(100);
        modeloDaColuna.getColumn(4).setMaxWidth(100);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbProdutos;
    private javax.swing.JTextField tfCodBarras;
    private javax.swing.JTextField tfDescricao;
    private javax.swing.JTextField tfPreco;
    private javax.swing.JTextField tfQuant;
    private javax.swing.JTextField tfQuantidade;
    private javax.swing.JTextField tfSubtotal;
    private javax.swing.JTextField tfTotalItem;
    // End of variables declaration//GEN-END:variables
}
